home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Atari Mega Archive 2
/
Atari Mega Archive CD - Volume 2.iso
/
minix
/
up1510b.tgz
/
up1510b
/
src
/
commands
/
more.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-07-23
|
38KB
|
1,938 lines
/*
* Copyright (c) 1980 Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted
* provided that the above copyright notice and this paragraph are
* duplicated in all such forms and that any documentation,
* advertising materials, and other materials related to such
* distribution and use acknowledge that the software was developed
* by the University of California, Berkeley. The name of the
* University may not be used to endorse or promote products derived
* from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
/*
** more.c - General purpose tty output filter and file perusal program
**
** by Eric Shienbrood, UC Berkeley
**
** modified by Geoff Peck, UCB to add underlining, single spacing
** modified by John Foderaro, UCB to add -c and MORE environment variable
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <ctype.h>
#include <signal.h>
#include <sgtty.h>
#include <setjmp.h>
#include <stdio.h>
#undef SIGTSTP /* POSIX requires it to be defined */
#ifndef _MINIX
#include <sys/param.h>
#include <sys/file.h>
#endif
#include <a.out.h>
/* varargs ------- */
/* varargs.h */
typedef char *va_list;
#define va_dcl int va_alist;
#define va_start(p) (p) = (va_list) &va_alist;
#define va_arg(p,type) ( (type *) ((p)+=sizeof(type)) )[-1]
#define va_end(p)
#define vfPrintf _doPrintf
#define vPrintf(fmt,args) vfPrintf(stdout,fmt,args)
/* end of varargs.h -------- */
#ifdef _MINIX
#include <limits.h>
#endif /* _MINIX */
#define HELPFILE "/usr/lib/more.help"
#define VI "/usr/bin/vi"
#define Fopen(s,m) (Currline = 0,file_pos=0,fopen(s,m))
#define Ftell(f) file_pos
#define Fseek(f,off) (file_pos=off,fseek(f,off,0))
#define Getc(f) (++file_pos, getc(f))
#define Ungetc(c,f) (--file_pos, ungetc(c,f))
#ifndef TIOCSETN
#define TIOCSETN TIOCSETP /* Minix doesn't have SETN */
#endif
#ifndef TBDELAY
#define TBDELAY 0006000 /* Minix doesn't have TBDELAY */
#endif
#define MBIT CBREAK
#define stty(fd,argp) ioctl(fd,TIOCSETN,argp)
#define TBUFSIZ 1024
#define LINSIZ 256
#define ctrl(letter) (letter & 077)
#define RUBOUT '\177'
#define ESC '\033'
#define QUIT '\034'
int Printf();
struct sgttyb otty, savetty;
void onquit(), end_it(), onsusp();
long file_pos, file_size;
int fnum, no_intty, no_tty, slow_tty;
int dum_opt, dlines, chgwinsz();
int nscroll = 11; /* Number of lines scrolled by 'd' */
int fold_opt = 1; /* Fold long lines */
int stop_opt = 1; /* Stop after form feeds */
int ssp_opt = 0; /* Suppress white space */
int ul_opt = 1; /* Underline as best we can */
int promptlen;
int Currline; /* Line we are currently at */
int startup = 1;
int firstf = 1;
int notell = 1;
int docrterase = 0;
int docrtkill = 0;
int bad_so; /* True if overwriting does not turn off standout */
int inwait, Pause, errors;
int within; /* true if we are within a file,
false if we are between files */
int hard, dumb, noscroll, hardtabs, clreol, eatnl;
int catch_susp; /* We should catch the SIGTSTP signal */
char **fnames; /* The list of file names */
int nfiles; /* Number of files left to process */
char *shell; /* The name of the shell to use */
int shellp; /* A previous shell command exists */
char ch;
jmp_buf restore;
char Line[LINSIZ]; /* Line buffer */
int Lpp = 24; /* lines per page */
char *Clear; /* clear screen */
char *eraseln; /* erase line */
char *Senter, *Sexit;/* enter and exit standout mode */
char *ULenter, *ULexit; /* enter and exit underline mode */
char *chUL; /* underline character */
char *chBS; /* backspace character */
char *Home; /* go to home */
char *cursorm; /* cursor movement */
char cursorhome[40]; /* contains cursor movement to home */
char *EodClr; /* clear rest of screen */
char *tgetstr();
char *tgoto();
int Mcol = 80; /* number of columns */
int Wrap = 1; /* set if automargins */
int soglitch; /* terminal has standout mode glitch */
int ulglitch; /* terminal has underline mode glitch */
int pstate = 0; /* current UL state */
char *getenv();
struct {
long chrctr, line;
} context, screen_start;
main(argc, argv)
int argc;
char *argv[];
{
register FILE *f;
register char *s;
register char *p;
register char ch;
register int left;
int prnames = 0;
int initopt = 0;
int srchopt = 0;
int clearit = 0;
int initline;
char initbuf[80];
FILE *checkf();
nfiles = argc;
fnames = argv;
initterm ();
nscroll = Lpp/2 - 1;
if (nscroll <= 0)
nscroll = 1;
if(s = getenv("MORE")) argscan(s);
while (--nfiles > 0) {
if ((ch = (*++fnames)[0]) == '-') {
argscan(*fnames+1);
}
else if (ch == '+') {
s = *fnames;
if (*++s == '/') {
srchopt++;
for (++s, p = initbuf; p < initbuf + 79 && *s != '\0';)
*p++ = *s++;
*p = '\0';
}
else {
initopt++;
for (initline = 0; *s != '\0'; s++)
if (isdigit (*s))
initline = initline*10 + *s -'0';
--initline;
}
}
else break;
}
/* allow clreol only if Home and eraseln and EodClr strings are
* defined, and in that case, make sure we are in noscroll mode
*/
if(clreol)
{
if((Home == NULL) || (*Home == '\0') ||
(eraseln == NULL) || (*eraseln == '\0') ||
(EodClr == NULL) || (*EodClr == '\0') )
clreol = 0;
else noscroll = 1;
}
if (dlines == 0)
dlines = Lpp - (noscroll ? 1 : 2);
left = dlines;
if (nfiles > 1)
prnames++;
if (!no_intty && nfiles == 0) {
char *rindex();
p = rindex(argv[0], '/');
fputs("Usage: ",stderr);
fputs(p ? p + 1 : argv[0],stderr);
fputs(" [-dfln] [+linenum | +/pattern] name1 name2 ...\n",stderr);
exit(1);
}
else
f = stdin;
if (!no_tty) {
signal(SIGQUIT, onquit);
signal(SIGINT, end_it);
#ifdef SIGWINCH
signal(SIGWINCH, chgwinsz);
#endif
#ifdef SIGTSTP
if (signal (SIGTSTP, SIG_IGN) == SIG_DFL) {
signal(SIGTSTP, onsusp);
catch_susp++;
}
#endif /* SIGTSTP */
stty (fileno(stderr), &otty);
}
if (no_intty) {
if (no_tty)
copy_file (stdin);
else {
if ((ch = Getc (f)) == '\f')
doclear();
else {
Ungetc (ch, f);
if (noscroll && (ch != EOF)) {
if (clreol)
home ();
else
doclear ();
}
}
if (srchopt)
{
search (initbuf, stdin, 1);
if (noscroll)
left--;
}
else if (initopt)
skiplns (initline, stdin);
screen (stdin, left);
}
no_intty = 0;
prnames++;
firstf = 0;
}
while (fnum < nfiles) {
if ((f = checkf (fnames[fnum], &clearit)) != NULL) {
context.line = context.chrctr = 0;
Currline = 0;
if (firstf) setjmp (restore);
if (firstf) {
firstf = 0;
if (srchopt)
{
search (initbuf, f, 1);
if (noscroll)
left--;
}
else if (initopt)
skiplns (initline, f);
}
else if (fnum < nfiles && !no_tty) {
setjmp (restore);
left = command (fnames[fnum], f);
}
if (left != 0) {
if ((noscroll || clearit) && (file_size != LONG_MAX))
if (clreol)
home ();
else
doclear ();
if (prnames) {
if (bad_so)
erase (0);
if (clreol)
cleareol ();
pr("::::::::::::::");
if (promptlen > 14)
erase (14);
Printf ("\n");
if(clreol) cleareol();
Printf("%s\n", fnames[fnum]);
if(clreol) cleareol();
Printf("::::::::::::::\n");
if (left > Lpp - 4)
left = Lpp - 4;
}
if (no_tty)
copy_file (f);
else {
within++;
screen(f, left);
within = 0;
}
}
setjmp (restore);
fflush(stdout);
fclose(f);
screen_start.line = screen_start.chrctr = 0L;
context.line = context.chrctr = 0L;
}
fnum++;
firstf = 0;
}
reset_tty ();
exit(0);
}
argscan(s)
char *s;
{
int seen_num = 0;
while (*s != '\0') {
switch (*s) {
case '0': case '1': case '2':
case '3': case '4': case '5':
case '6': ca